package com.sky.rabc.config;

import com.sky.rabc.common.RedissonCommon;
import com.sky.rabc.mapper.BackPermissionMapper;
import com.sky.rabc.mapper.BackRoleMapper;
import com.sky.rabc.vo.LoginBackPermissionVO;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 程序加载时将角色为key，与角色相关的权限放入redis中
 */
@Slf4j
@Component
public class StartupConfig {
    private final RedissonCommon redissonCommon;
    private final BackPermissionMapper backPermissionMapper;
    private final BackRoleMapper backRoleMapper;

    public StartupConfig(RedissonCommon redissonCommon, BackPermissionMapper backPermissionMapper, BackRoleMapper backRoleMapper) {
        this.redissonCommon = redissonCommon;
        this.backPermissionMapper = backPermissionMapper;
        this.backRoleMapper = backRoleMapper;
    }

    @PostConstruct
    public void init() {
        log.info("start:{}", "redis数据注入");
        List<Long> allIds = backRoleMapper.getAllIds();
        for (Long roleId : allIds) {
            List<LoginBackPermissionVO> backPermissionListByRoleId = backPermissionMapper.getBackPermissionListByRoleId(roleId);
            redissonCommon.setValue("role_permissions:" + roleId, backPermissionListByRoleId, null);
        }
    }
}
